home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dtk_demo.zip / DIR_ATTR.C < prev    next >
C/C++ Source or Header  |  1991-09-12  |  3KB  |  122 lines

  1. /*  DIR_ATTR.C
  2.  *  counts files matching a filename specification and
  3.  *  a file attribute specification and displays
  4.  *  the filenames and attributes in columns
  5.  *  last mod.: 31-AUG-91
  6.  */
  7.  
  8. #include <STDIO.H>
  9. #include <STDLIB.H>
  10. #include <STRING.H>
  11. #include <CONIO.H>
  12.  
  13. #include <L_FILE.H>
  14. #include <L_MISC.H>
  15. #include <L_DIR.H>
  16.  
  17. char *usage = "Use: DIR_ATTR file_spec [attribute_spec]\n"
  18.               "file attributes: RHSVDA, ! = not, "
  19.               "e.g. DIR_ATTR K*.* AS!R\n";
  20.  
  21. /*  the file attribute specification consists of a string
  22.  *  of the form [str1][!str2] where str1 consists of one or more of
  23.  *  'R', 'H', 'S', 'V', 'D' and 'A', which are interpreted as:
  24.  *       R = read only
  25.  *       H = hidden
  26.  *       S = system
  27.  *       V = volume label
  28.  *       D = directory
  29.  *       A = archive
  30.  *  the program ignores other letters;
  31.  *  str1!str2 means: having one or more of the attributes
  32.  *  in str1 and none of the attributes in str2;
  33.  *  e.g. "D!H" means: all directories which are not hidden
  34.  *  and "!HSVD" means: all files except those which are hidden,
  35.  *  system, volume labels or directories;
  36.  *  omitting the attribute specification gives all files
  37.  *  except hidden and system files and volume names
  38.  */
  39.  
  40. #define MAX_FILES 100
  41.  
  42. File files[MAX_FILES];
  43. char attr[] = "RHSVDA";
  44. char file_spec[81];
  45. char attr_spec[81];
  46.  
  47. /*  to avoid the limit on the number of files we
  48.  *  dynamically allocate memory for the array of Files
  49.  */
  50.  
  51. /*---------------------------*/
  52. void main(int argc,char **argv)
  53. {
  54. int i, j, k, num, num1, num_lines, ch;
  55. int count[6];
  56.  
  57. for ( i=0; i<6; i++ )
  58.     count[i] = 0;
  59.  
  60. if ( argc < 2 )
  61.     {
  62.     printf(usage);
  63.     return;
  64.     }
  65. else
  66.     strcpy(file_spec,strupr(argv[1]));
  67.  
  68. if ( argc < 3 )
  69.     strcpy(attr_spec,"!HSV");
  70. else
  71.     strcpy(attr_spec,strupr(argv[2]));
  72.  
  73. if ( ( num = num_matching_files(file_spec,attr_spec) ) == -1 )
  74.     printf("\nInvalid file attribute specification.\n");
  75. else if ( !num )
  76.     printf("\nNo files.\n");
  77. else if ( num > MAX_FILES )
  78.     printf("\nMore than %d files.\n",MAX_FILES);
  79.  
  80. if ( num <= 0 || num > MAX_FILES )
  81.     return;
  82.  
  83. num1 = 4*(((num-1)/4)+1);
  84.  
  85. printf("\nFiles %s with attributes %s: %d\n",
  86.     file_spec,(*attr_spec=='\0'?"\"\"":attr_spec),num);
  87.  
  88. read_directory(file_spec,attr_spec,files);
  89.  
  90. num_lines = num1/4;
  91.  
  92. for ( i=0; i<num1; i++ )
  93.     {
  94.     if ( !output_redirected() && i>0 && !(i%92) )
  95.         {
  96.         printf("--More--");
  97.         getch();
  98.         for ( j=0; j<8; j++ )
  99.             putchar('\b');
  100.         }
  101.     k = i/4 + num_lines*(i%4);
  102.     if ( k < num )
  103.         {
  104.         printf("%12s ",files[k].name);
  105.         for ( j=0; j<6; j++ )
  106.             {
  107.             ch = ( (files[k].attribute>>j) & 1 ? attr[j] : '.' );
  108.             if ( ch != '.' )
  109.                 count[j]++;
  110.             putchar(ch);
  111.             }
  112.         putchar( (i+1)%4 ? ' ' : '\n' );
  113.         }
  114.     else
  115.         putchar('\n');
  116.     }
  117.  
  118. for ( j=0; j<6; j++ )
  119.     printf(" %c=%d  ",attr[j],count[j]);
  120. printf("\n");
  121. }
  122.